package com.pocketdigi.plib.core;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import org.androidannotations.annotations.EFragment;

/**
 * Created by fhp on 14-9-1.
 */
public abstract class PFragment extends Fragment implements OnBackPressedListener {
    PFragmentActivity parentActivity;
    Bundle resultArgs;
    int requestCode;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        PLog.d(this, "onCreate");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        PLog.d(this, "onCreateView");
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        PLog.d(this, "onViewCreated");
        super.onViewCreated(view, savedInstanceState);
    }
    /**
     * 当Fragment在栈中，从其他Fragment返回到该Fragment时，通过该方法传递参数，类同onActivityResult
     * 但因为该方法在onCreateView前调用，无法设置UI，所以要在AfterViews里判断getResultArgs()是否为空再做处理
     * @param requestCode 可以通过requestCode判断数据来源
     * @param args 参数
     */
    public final void onResult(int requestCode,Bundle args){
        this.resultArgs=args;
        this.requestCode =requestCode;
    }

    /**
     * 获取返回时携带的参数
     * @return
     */
    public Bundle getResultArgs() {
        return resultArgs;
    }

    /**
     * 获取RequestCode
     * @return
     */
    public int getRequestCode() {
        return requestCode;
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        PLog.d(this, "onDestroyView");
        resultArgs=null;
        requestCode =0;
    }

    @Override
    public void onDestroy() {
        PLog.d(this, "onDestroy");
        super.onDestroy();
    }

    @Override
    public void onStart() {
        super.onStart();
        PLog.d(this, "onStart");
    }

    @Override
    public void onPause() {
        super.onPause();
        PLog.d(this, "onPause");
        unregisterListerOrReceiver();
    }

    @Override
    public void onResume() {
        super.onResume();
        registerListenerOrReceiver();
    }

    @Override
    public void onStop() {
        super.onStop();
        PLog.d(this, "onStop");
    }

    /**
     * 注册监听器以及接收器(包括Event),在Fragment被隐藏或销毁时，会调用unregisterListerOrReceiver
     */
    protected void registerListenerOrReceiver() {
        FragmentActivity activity = getActivity();
        if (activity instanceof PFragmentActivity) {
            parentActivity = (PFragmentActivity) activity;
            parentActivity.addBackPressedListener(this);
        }
    }

    /**
     * 解注册监听器及接收器(包括Event)*
     */
    protected void unregisterListerOrReceiver() {
        if (parentActivity != null) {
            parentActivity.removeBackPressedListener(this);
        }
        parentActivity = null;
    }

    @Override
    public boolean onBackPressed() {
        return false;
    }
}